//
// Created by shen on 2024/10/30.
//

#ifndef CROSSPLATFORMPLAYER_VIDEODECODER_H
#define CROSSPLATFORMPLAYER_VIDEODECODER_H
#include <DecoderBase.h>
#include <cstring>

#include "VideoRender.h"
extern "C"{
#include <libavutil/imgutils.h>
    }
class VideoDecoder: public  DecoderBase{
public:
    VideoDecoder(char *url){
        Init(url, AVMEDIA_TYPE_VIDEO);
    }

    virtual ~VideoDecoder(){
        UnInit();
    }

    int GetVideoWidth()
    {
        return m_VideoWidth;
    }
    int GetVideoHeight()
    {
        return m_VideoHeight;
    }

    void SetVideoRender(VideoRender *videoRender)
    {
        m_VideoRender = videoRender;
    }
private:
    mutex               m_Mutex1;

    virtual void OnDecoderReady();
    virtual void OnDecoderDone();
    virtual void OnFrameAvailable(AVFrame *frame);
    void saveFrame(AVFrame *pFrame, int width, int height, int iFrame);
    int yuv_png(AVFrame *pFrame,AVCodecContext *pCodecCtx,int i);
    int m_VideoWidth = 0;
    int m_VideoHeight = 0;

    int m_RenderWidth = 0;
    int m_RenderHeight = 0;
    const AVPixelFormat DST_PIXEL_FORMAT = AV_PIX_FMT_RGBA;
    AVFrame *m_RGBAFrame = nullptr;
    uint8_t *m_FrameBuffer = nullptr;
    SwsContext *m_SwsContext = nullptr;
    VideoRender *m_VideoRender = nullptr;
};


#endif //CROSSPLATFORMPLAYER_VIDEODECODER_H
